/*
 * This file is part of the OpenSCADA project
 * Copyright (C) 2006-2010 TH4 SYSTEMS GmbH (http://th4-systems.com)
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.openscada.opc.lib.da;

import org.jinterop.dcom.common.JIException;
import org.jinterop.dcom.core.JIVariant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Item
{
    private static Logger _log = LoggerFactory.getLogger ( Item.class );

    private Group _group = null;

    private int _serverHandle = 0;

    private int _clientHandle = 0;

    private String _id = null;

    Item ( final Group group, final int serverHandle, final int clientHandle, final String id )
    {
        super ();
        _log.debug ( String.format ( "Adding new item '%s' (0x%08X) for group %s", id, serverHandle, group.toString () ) );
        this._group = group;
        this._serverHandle = serverHandle;
        this._clientHandle = clientHandle;
        this._id = id;
    }

    public Group getGroup ()
    {
        return this._group;
    }

    public int getServerHandle ()
    {
        return this._serverHandle;
    }

    public int getClientHandle ()
    {
        return this._clientHandle;
    }

    public String getId ()
    {
        return this._id;
    }

    public void setActive ( final boolean state ) throws JIException
    {
        this._group.setActive ( state, this );
    }

    public ItemState read ( final boolean device ) throws JIException
    {
        return this._group.read ( device, this ).get ( this );
    }

    public Integer write ( final JIVariant value ) throws JIException
    {
        return this._group.write ( new WriteRequest[] { new WriteRequest ( this, value ) } ).get ( this );
    }
}
